package com.syt.order.controller.api;

import com.syt.enums.PaymentTypeEnum;
import com.syt.order.service.PaymentInfoService;
import com.syt.order.service.WeiXinService;
import com.syt.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: xxx
 * @Date: 2023/2/28 19:50
 * @Description: 关于微信方面的接口
 */
@Api(tags = "微信接口")
@RestController
@RequestMapping("/api/order/weixin")
public class WeiXinApiController {

    @Autowired
    private WeiXinService weiXinService;
    @Autowired
    private PaymentInfoService paymentInfoService;

    @ApiOperation(value = "查询支付状态")
    @GetMapping("/queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable Long orderId){
        //查询微信支付状态
        Map<String,String> resultMap=weiXinService.queryPayStatus(orderId);
        //判断
        if (resultMap==null){
            return Result.ok().message("支付出错");
        }
        if ("SUCCESS".equals(resultMap.get("trade_state"))){
            //更新订单状态
            String out_trade_no = resultMap.get("out_trade_no");//订单单号
            paymentInfoService.paySuccess(out_trade_no, PaymentTypeEnum.WEIXIN.getStatus(), resultMap);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }

    @ApiOperation(value = "生成微信二维码")
    @GetMapping("/createNative/{orderId}")
    public Result createQRCode(@PathVariable Long orderId) {
       Map map= weiXinService.createQRCode(orderId);
       return Result.ok(map);
    }


}
